package com.podcstr.service;

import com.podcstr.dao.CommentDao;
import com.podcstr.entity.Comment;
import com.podcstr.service.exception.ServiceException;
import org.apache.log4j.Logger;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author azim
 * @since 6/2/13 
 */
@Local
@Stateless
public class CommentServiceImpl implements CommentService {

    private final Logger log = Logger.getLogger(CommentServiceImpl.class.getName());

    @Inject
    private CommentDao commentDao;

    @Override
    public void add(Comment comment) throws ServiceException {

        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set violations = validator.validate(comment);
        if (violations.size() > 0) {

            log.error(violations);
            throw new ServiceException(violations);
        }

        commentDao.add(comment);

        log.info(new Date() + " : Added a new comment with id : " + comment.getId());
    }

    @Override
    public void delete(long commentId) {

        commentDao.delete(commentId);
        log.info(new Date() + " : Deleted a comment with id : " + commentId);
    }

    @Override
    public List<Comment> findByPodcastId(long podcastId) {

        log.info(new Date() + " : Returning  a list comment with podcast id : " + podcastId);
        return commentDao.findByPodcastId(podcastId);
    }

    @Override
    public Comment findById(long commentId) {

        log.info(new Date() + " : Returning a comment with id : " + commentId);
        return commentDao.findById(commentId);
    }
}
